package picselect;

import android.app.Activity;
import android.content.Intent;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.aliang.auto.R;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

import app.auto.AndroidInstance;
import app.auto.runner.base.intf.FunCallback;

import static android.app.Activity.RESULT_OK;

public class PicSelector extends AndroidInstance {
//
//    //选择弹出框
//    public void choosePhoto(int type,Activity aty) {
//
//        DialogUtils.getInstance().with(aty)
//                .setlayoutPosition(Gravity.BOTTOM)
//                .setlayoutPading(30, 0, 30, 60)
//                .setlayoutAnimaType(DialogUtils.ANIM_FROM_BOTTOM)
//                .setlayoutId(R.layout.dialog_choose_photo)
//                .setlayoutPading(0, 0, 0, 0)
//                .setOnChildViewclickListener((view, layoutResId) -> {
//                    TextView album = view.findViewById(R.id.dialog_choose_album);
//                    TextView camera = view.findViewById(R.id.dialog_choose_camera);
//                    TextView cancel = view.findViewById(R.id.dialog_choose_cancel);
//                    //相册
//                    album.setOnClickListener(v -> {
//                        DialogUtils.dismiss();
//                        getPhoto(false, type);
//                    });
//                    //拍照
//                    camera.setOnClickListener(v -> {
//                        DialogUtils.dismiss();
//                        getPhoto(true, type);
//                    });
//
//                    cancel.setOnClickListener(v -> DialogUtils.dismiss());
//
//                })
//                .show();
//
//    }


    FunCallback funCallback;

    public PicSelector setFunCallback(FunCallback funCallback) {
        this.funCallback = funCallback;
        return this;
    }

    /**
     * 选择照片
     */
    private void getPhoto(boolean isCamera) {
        if (isCamera) {

            PictureSelector.create((Activity) getContext())
                    .openCamera(SelectMimeType.ofImage())
                    .forResultActivity(PictureConfig.CHOOSE_REQUEST);

//            PictureSelector.create((Activity) getContext())
//                    .openCamera(
//                            PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
//                    .maxSelectNum(1)// 最大图片选择数量 int
//                    .imageSpanCount(4)// 每行显示个数 int
//                    .loadImageEngine(GlideEngine.createGlideEngine())
//                    .previewImage(true)// 是否可预览图片 true or false
//                    .isCamera(false)// 是否显示拍照按钮 true or false
//                    .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
//                    .compress(true)// 是否压缩 true or false
//                    .theme(R.style.pink_style)
//                    .forResult(PictureConfig.CHOOSE_REQUEST);
        } else {

            PictureSelector.create(getContext())
                    .openGallery(SelectMimeType.ofImage())
                    .setImageEngine(GlideEngine.createGlideEngine())
                    .forResult(PictureConfig.CHOOSE_REQUEST);
//
//            PictureSelector.create((Activity) getContext())
//                    .openGallery(PictureMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
//                    .maxSelectNum(1)// 最大图片选择数量 int
//                    .imageSpanCount(4)// 每行显示个数 int
//                    .loadImageEngine(GlideEngine.createGlideEngine())
//                    .previewImage(true)// 是否可预览图片 true or false
//                    .isCamera(false)// 是否显示拍照按钮 true or false
//                    .imageFormat(PictureMimeType.PNG)// 拍照保存图片格式后缀,默认jpeg
//                    .compress(true)// 是否压缩 true or false
//                    .theme(R.style.pink_style)
        }
    }

    public void choosePicture(FunCallback<String> funCallback) {
        DialogUtils.getInstance()
                .with(getContext())
                .setlayoutPosition(Gravity.BOTTOM)
                .setlayoutPading(30, 0, 30, 60)
                .setlayoutAnimaType(DialogUtils.ANIM_FROM_BOTTOM)
                .setlayoutId(R.layout.dialog_choose_photo)
                .setlayoutPading(0, 0, 0, 0)
                .setOnChildViewclickListener(new DialogUtils.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {

                        TextView album = view.findViewById(R.id.dialog_choose_album);
                        TextView camera = view.findViewById(R.id.dialog_choose_camera);
                        TextView cancel = view.findViewById(R.id.dialog_choose_cancel);
                        //相册
                        album.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                DialogUtils.dismiss();
                                setFunCallback(funCallback).getPhoto(false);
                            }
                        });
                        //拍照
                        camera.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                DialogUtils.dismiss();
                                setFunCallback(funCallback).getPhoto(true);
                            }
                        });

                        cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                DialogUtils.dismiss();
                            }
                        });
                    }
                })
                .show();
    }

    public String mPhotoPath;

    public String getmPhotoPath() {
        return mPhotoPath;
    }

    public void setmPhotoPath(String mPhotoPath) {
        this.mPhotoPath = mPhotoPath;
    }

    public void onActivityResultInner(int requestCode, int resultCode, Intent data, List<LocalMedia> selectList, FunCallback<String> funCallback) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    if (selectList == null) {
                        selectList = PictureSelector.obtainSelectorList(data);
                    }
                    if (selectList.size() > 0) {
                        LocalMedia localMedia = selectList.get(0);
                        if (localMedia.isCompressed()) {
                            mPhotoPath = localMedia.getCompressPath();
                        }else{
                            mPhotoPath = localMedia.getRealPath();
                        }
                        if (funCallback != null) {
                            funCallback.onCallback(mPhotoPath, null);
                        }

                    }
                    break;

            }
        }
    }

    @Override
    public AndroidInstance use(Object mp, Object... p) {
        return null;
    }
}
